var express = require('express');
var jwt = require('jsonwebtoken')
var router = express.Router();
var { landlordModel,
    houseModel,
    appointmentModel,
    questionModel,answerModel,collectModel } = require('../model/model')
//获取预约表
router.get("/getAppointment",async(req,res)=>{
    let uid = req.query.uid
    let appointstatus = req.query.btnindex
    // console.log(appointstatus)
    let list = await appointmentModel.find().populate('hid')
    let data = list.filter(item=>item.uid===uid)
    if(appointstatus!=="0" && appointstatus){
        // console.log(appointstatus)
        data = data.filter(item=>item.appointstatus===appointstatus-1)
    }
    res.send(data)
})

//获取收藏列表
router.get("/getCollect",async(req,res)=>{
    let data = await collectModel.find()
    res.send(data)
})
//收藏列表删除
router.post('/upcollect',async(req,res)=>{
    let item=req.body
    console.log(item)
    // let _id = ObjectId(item._id)
    await houseModel.updateOne({_id:item.houseid},{is_collect:false})
    await collectModel.deleteOne({_id:item._id})
    
    res.send({
      code:200,
      msg:'更新成功'
    })
  })
//预约表删除
router.post('/delappointemnt',async(req,res)=>{
    let item=req.body
    console.log(item)
    await appointmentModel.deleteOne({_id:item._id})
    res.send({
      code:200,
      msg:'更新成功'
    })
  })
  //房屋表修改
  router.post('/edithouse',async(req,res)=>{
    let item=req.body
    let _id = item._id
    console.log(item)
    await houseModel.updateOne({_id:_id},item)
    res.send({
      code:200,
      msg:'更新成功'
    })
  })
  //获取房屋列表
router.get("/getHome",async(req,res)=>{
    let lid = req.query.uid
    let house_status = req.query.btnindex
    console.log(house_status)
    let arr = await houseModel.find()
    // console.log(1111)
    let data = arr.filter(item=>item.lid==lid)
    if(house_status!=="0" && house_status){
        console.log(1111111111)
        data = data.filter(item=>item.house_status==house_status-1)
        console.log(data)
    }
    res.send(data)
})
//房东预约表
router.get("/dongAppointment",async(req,res)=>{
  let uid = req.query.uid
  let yue_status = req.query.btnindex
  console.log(yue_status)
  let list = await appointmentModel.find().populate('hid')
  let data = list.filter(item=>item.uid===uid)
  if(yue_status!=="0" && yue_status){
      // console.log(appointstatus)
      data = data.filter(item=>item.yue_status===yue_status-1)
  }
  res.send(data)
})
//房东被预约表处理
router.post('/editAppointment',async(req,res)=>{
  let item=req.body
  let _id = req.body._id
  let hid = item.hid._id
  await houseModel.updateOne({_id:hid},{house_status:1})
  await appointmentModel.updateOne({_id:_id},item)
  res.send({
    code:200,
    msg:'更新成功'
  })
})
module.exports = router;